package com.matt.controller.api.web;

import com.matt.domain.Clientele;
import com.matt.domain.ContractHistory;
import com.matt.foundation.constant.ClienteleConstant;
import com.matt.foundation.constant.ContractConstant;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.model.StatusResponse;
import com.matt.foundation.vo.ContractVo;
import com.matt.service.ClienteleService;
import com.matt.service.ContractHistoryService;
import com.matt.service.ContractService;
import com.matt.remoteCall.SsoCallService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * **********************
 * description :回款记录控制器
 * Created by Ron
 * from com.matt
 * on 2018/6/19 0019.
 * **********************
 */
@RestController
@RequestMapping("/web/contract_history")
@Api(tags = "CRM - 合同跟进历史（回款记录）")
public class ContractHistoryController {

    @Autowired
    private ContractHistoryService contractHistoryService;

    @Autowired
    private ContractService contractService;

    @Autowired
    private ClienteleService clienteleService;

    @Autowired
    private SsoCallService ssoCallService;

    @RequestMapping(value = "/add")
    @ApiOperation(value = "增加合同跟进历史", httpMethod = "POST")
    public StatusResponse add(@RequestHeader String ticket, ContractHistory contractHistory) {
    	
    	ContractVo co = contractService.details(contractHistory.getContractId());
    	    	
    	if(co.getPayStatus() == ContractConstant.PayStatus.PAID.codeOf()) {
    		return StatusResponse.error("000502", "您的合同已经补款完毕，请勿重复提交");
    	}
    	
    	if(contractHistory.getBackMoney().compareTo(co.getTotalAmount().subtract(co.getPracticalAmount())) == 1) {
    		return StatusResponse.error("000503", "您提交的金额大于合同待回款金额，请重新提交");
    	}
    	
    	
        StaffDTO baseInfo = ssoCallService.getStaffDTO(ticket);
        
        String staffId = baseInfo.getId();
        String realName = baseInfo.getRealName();
        String imgUrl = baseInfo.getHeadImgUrl();

        ContractHistory vo = contractHistoryService.save(staffId, realName, imgUrl, "", contractHistory.getContractId(), contractHistory.getBackMoney());

        //查询客户合同不为已中止的已付清的合同
        Long count = contractService.findByStatusAndPayStatus(vo.getClienteleId(), ContractConstant.Status.STOP.codeOf(), ContractConstant.PayStatus.NON_PAY.codeOf());
        Clientele clientele = clienteleService.getById(vo.getClienteleId());
        if (count > 0)
            clientele.setStatus(ClienteleConstant.Status.ORDER_CASH.codeOf());
        else
            clientele.setStatus(ClienteleConstant.Status.SUCCESS.codeOf());

        clienteleService.saveOrigin(clientele);
        return StatusResponse.success(vo);
    }

    @RequestMapping(value = "/find_by_contract")
    @ApiOperation(value = "根据合同查询跟进历史", httpMethod = "GET")
    public StatusResponse findByContractId(@RequestHeader String ticket, String contractId) {
        return StatusResponse.success(contractHistoryService.findByContractId(contractId));
    }


}
